﻿Public Class Client

    Private _AddressIP As String
    Private _WorkSocket As Socket
    Private _Buffer(10000) As Byte
    Private _BufferText As String
    Private _Index As Integer

#Region "Properties"

    Public Property AddressIP As String
        Get
            Return _AddressIP
        End Get
        Set(ByVal value As String)
            _AddressIP = value
        End Set
    End Property

    Public Property WorkSocket As Socket
        Get
            Return _WorkSocket
        End Get
        Set(ByVal value As Socket)
            _WorkSocket = value
        End Set
    End Property

    Public Property Buffer As Byte()
        Get
            Return _Buffer
        End Get
        Set(ByVal value As Byte())
            _Buffer = value
        End Set
    End Property

    Public Property BufferText As String
        Get
            Return _BufferText
        End Get
        Set(ByVal value As String)
            _BufferText = value
        End Set
    End Property

    Public Property Index As Integer
        Get
            Return _Index
        End Get
        Set(ByVal value As Integer)
            _Index = value
        End Set
    End Property

#End Region

    Public Function IsConnected()
        If Not _WorkSocket Is Nothing And _WorkSocket.Connected Then
            Return True
        Else
            Return False
        End If
    End Function

    Public Sub SendData(ByVal Data As String)
        If IsConnected() Then
            Dim Bytes() As Byte = Encoding.UTF8.GetBytes(Data)
            _WorkSocket.BeginSend(Bytes, SocketFlags.None, New AsyncCallback(AddressOf SendCallBack), _WorkSocket)
        End If
    End Sub

    Private Sub SendCallBack(ByVal IResult As IAsyncResult)

        Try

            Dim Handler As Socket = CType(IResult.AsyncState, Socket)
            Dim SendSize As Integer = Handler.EndSend(IResult)

        Catch ex As Exception
            AddText(ex.Message)
        End Try

    End Sub

End Class
